package cn.jilian.meta.service.impl;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.collect.Lists;

import cn.jilian.entity.Meta;
import cn.jilian.meta.handler.CompGenerator;
import cn.jilian.meta.handler.GeneratorManager;
import cn.jilian.meta.handler.domain.CellEditorGrid;
import cn.jilian.meta.handler.domain.Container;
import cn.jilian.meta.handler.domain.Form;
import cn.jilian.meta.handler.domain.FuncCallExpression;
import cn.jilian.meta.handler.domain.Function;
import cn.jilian.meta.handler.domain.Grid;
import cn.jilian.meta.handler.domain.JsonStore;
import cn.jilian.meta.handler.domain.Model;
import cn.jilian.meta.service.ExtJsGenerator;

@Service
public class Extjs6Generator implements ExtJsGenerator {

	private GeneratorManager manager;
	
	private ObjectMapper objectMapper;
	
	private ObjectWriter writer;
	
	public static final String DEFINE_FUNC="Ext.define";
	
	public static final String CREATE_FUNC="Ext.create";
	
	public Extjs6Generator(@Autowired GeneratorManager manager,@Autowired ObjectMapper objectMapper) {
		this.manager=manager;
		this.objectMapper=objectMapper;
		this.writer=this.objectMapper.writerWithDefaultPrettyPrinter();
	}
	
	
	@Override
	public String genGrid(Meta meta) {
		try {
		CompGenerator<Grid> g=manager.getGenerator(Grid.class);
		Grid grid= g.convert(meta, null);
		return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,grid.getName(),grid));
		}catch(Exception e) {
			return "";
		}
	}

	@Override
	public String genJsonStore(Meta meta) {
		try {
			CompGenerator<JsonStore> g=manager.getGenerator(JsonStore.class);
			JsonStore grid= g.convert(meta, null);
			return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,grid.getName(),grid));
			}catch(Exception e) {
				e.printStackTrace();
				
				return "";
			}
	}

	@Override
	public String genForm(Meta meta) {
		try {
		CompGenerator<Form> f=manager.getGenerator(Form.class);
		Form form= f.convert(meta, null);
		return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,form.getName(),form));
		}catch(Exception e) {
			return "";
		}
	}

	@Override
	public String genModel(Meta meta) {
		try {
			CompGenerator<Model> g=manager.getGenerator(Model.class);
			Model model= g.convert(meta, null);
			return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,model.getName(),model));
			}catch(Exception e) {
				return "";
			}
	}


	@Override
	public void exportTree(Meta meta, OutputStream out) {
		
		
	}

	public static final String WRAP_LINE="\r\n";

	@Override
	public String genGridForm(Meta meta) {
		Map<String,Object> extConfig=new HashMap<>();
		extConfig.put("region", "west");
		CompGenerator<Grid> g=manager.getGenerator(Grid.class);
		Grid grid= g.convert(meta, extConfig);
		String body=new StringBuilder("var form=Ext.getCmp('');")
				.append(WRAP_LINE)
				.append("form.loadRecord(record);")
				.toString();
		grid.on("rowclick", new Function("table,record,element,rowIndex,e,eOpts",body));
		Map<String,Object> extConfig2=new HashMap<>();
		extConfig2.put("region", "west");
		extConfig2.put("title", "详情");
		CompGenerator<Form> f=manager.getGenerator(Form.class);
		Form form= f.convert(meta, extConfig2);
		CompGenerator<Container> containerGenerator=manager.getGenerator(Container.class);
		Container c=containerGenerator.convert(meta, extConfig2);
		c.setItems(Lists.newArrayList(grid,form));
		return null;
	}
	


	@Override
	public String genAddRecordWin(Meta meta) {
		CompGenerator<Form> f=manager.getGenerator(Form.class);
		Form form= f.convert(meta, null);
		form.setFloating(Boolean.TRUE);
		
		try {
			return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,form.getName(),form));
			
		}catch(Exception e) {
			
		}
		return null;
	}


	@Override
	public String genEditorGrid(Meta meta) {
		CompGenerator<CellEditorGrid> f=manager.getGenerator(CellEditorGrid.class);
		CellEditorGrid form= f.convert(meta, null);
		try {
			return writer.writeValueAsString(new FuncCallExpression(DEFINE_FUNC,form.getName(),form));
			
		}catch(Exception e) {
			
		}
		return null;
	}

}
